package servlet;

import entity.Life;
import service.LifeService;
import service.impl.LifeServiceImpl;
import util.ScannerUtils;

import java.util.ArrayList;

public class UserLife {
    LifeService lifeService = new LifeServiceImpl();
    public void userList(String idNumber) throws Exception{
        System.out.println("生活记录");
        System.out.println("请选择：");
        System.out.println("1.查看生活记录");
        System.out.println("2.添加生活记录");
        String choice = ScannerUtils.scannerUtil();
        if (choice.equals("1")){
            listLife(idNumber);
        } else if (choice.equals("2")) {
            addLife(idNumber,null);
        }
    }
    public void listLife(String idNumber) throws Exception{
        ArrayList<Life> list = lifeService.findLife(idNumber);
        for (int i = 0; i < list.size(); i++){
            System.out.println(list.get(i).getLife());
        }
        UserLife userLife = new UserLife();
        userLife.userList(idNumber);
    }
    public void addLife(String idNumber, String life) throws Exception{
        if (life == null){
            System.out.println("请输入新的生活记录");
            life = ScannerUtils.scannerUtil();
            lifeService.insert(idNumber,life);
            UserLife userLife = new UserLife();
            userLife.userList(idNumber);
        }else {
            lifeService.insert(idNumber,life);
        }
    }
}
